let s:NOTIFY = SpaceVim#api#import('notify')
let s:NOTIFY.notify_max_width = 100
let s:NOTIFY.timeout = 3000

let s:current_file = expand('<sfile>')
let s:current_directory = fnamemodify(s:current_file, ':p:h')
let s:python_directory = globpath(s:current_directory, 'python')

function! s:check_python() abort
    if has('python3')
        let g:MatlabPython = get(g:, 'MatlabPython', 'py3')
    else
        if has('python')
            let g:MatlabPython = get(g:, 'MatlabPython', 'py')
        else
            echoerr 'python cannot be found!'
            return v:false
        endif
    endif
    return v:true
endfunction

function! s:check_vim_python() abort
    if s:check_python()
        execute g:MatlabPython 'import vim'
        execute g:MatlabPython "sys.path.insert(0, vim.eval('s:python_directory'))"
        execute g:MatlabPython "from matlabserver import MatlabProxy"
        return v:true
    endif
    return v:false
endfunction

function! s:print(message) dict
    call s:NOTIFY.notify(a:message)
endfunction

let s:matlab = { '_is_binded': v:false, '_has_proxy': v:false, '_connected': v:false, 'print': function('s:print') }

function! s:matlab.bind() abort
    if s:check_vim_python()
        execute g:MatlabPython "matlab = vim.bindeval('s:matlab')"
        execute g:MatlabPython "matlab['_is_binded'] = 'matlab' in dir()" 
    endif
    return self._is_binded
endfunction

function! s:matlab.has_proxy() abort
    if self.bind() && !self._has_proxy
        execute g:MatlabPython "matlab_proxy = MatlabProxy(matlab)" 
        execute g:MatlabPython "matlab['_has_proxy'] = 'matlab_proxy' in dir()" 
    endif
    return s:matlab._has_proxy
endfunction

function! s:matlab.is_connected() abort
    if self.has_proxy()
        execute g:MatlabPython "matlab['_connected'] = matlab_proxy.is_connected()"
    endif
    return has_key(s:matlab, '_connected') && s:matlab._connected
endfunction

function! s:matlab.connect() abort
    if !self.is_connected()
        try
            if self.has_proxy()
                execute g:MatlabPython "matlab_proxy.connect()"
            endif
        catch /.*/
            echoerr v:exception
        endtry
    endif
    return self.is_connected()
endfunction

function! s:matlab.execute(cmd) abort
    if self.connect()
        execute g:MatlabPython "matlab_proxy.execute(vim.eval('a:cmd'))"
    endif
endfunction

function! bootstrap#matlab#get() abort
    return s:matlab
endfunction

function! bootstrap#matlab#run_file(...) abort
    if a:0 < 1
        let l:file = fnamemodify(bufname('%'), ':p')
    else
        let l:file = a:1
    endif
    let l:filename = fnamemodify(l:file, ':t:r')
    let l:directory = fnamemodify(l:file, ':p:h')
    call s:NOTIFY.notify('matlab run file ' . l:filename . ' in directory ' . l:directory) 
    let l:script = 'cd ' . l:directory . ';'
    let idx = match(l:filename, '^test')
    if idx >= 0
        let l:script = l:script . "runtests('" . l:filename . "');"
    else
        let l:script = l:script . l:filename
    endif
    call s:NOTIFY.notify('execute: ' . l:script) 
    call s:matlab.execute(l:script)
endfunction

function! s:on_matlab() abort
    call bootstrap#message#print('on_matlab') 
    setlocal cms=%\ %s
    nnoremap <buffer> <F5> :MatlabRunFile<cr>
    syntax on
endfunction

function! bootstrap#matlab#setup() abort
    if !exists(':MatlabRunFile')
        command! -nargs=? -complete=buffer MatlabRunFile :call bootstrap#matlab#run_file(<f-args>)
    endif
    autocmd! FileType matlab call <SID>on_matlab()
    autocmd! BufRead,BufNewFile *.m set filetype=matlab
endfunction


